<template>
  <div class="container">
    <div class="app-container">
      组织
      <!-- render-after-expand 默认展开所有
        props:配置选项
      -->
      <el-tree :data="list" :props="{label:'name',children:'children'}" :default-expand-all="true">
        <template #default="{data}">
          <el-row style="width: 100%" type="flex" justify="space-between" align="middle">
            <el-col :span="8">{{ data.name }}</el-col>
            <el-col :span="8">
              <span style="margin-right: 10px;">{{ data.managerName }}</span>

              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加部门</el-dropdown-item>
                  <el-dropdown-item command="edid">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>

            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDepart :show-dialog.sync="showDialog" :current-id="currentId" @refresh="getDepartmentList" />
  </div>
</template>

<script>
import { getDepartmentList } from '@/api/department.js'
import { getTreeFromList } from '@/utils/index.js'
import AddDepart from './components/AddDepart.vue'
export default {
  name: 'Department',
  components: {
    AddDepart
  },
  data() {
    return {
      list: [],
      showDialog: false,
      currentId: -1 // 表示当前部门的id
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentList()
      this.list = getTreeFromList(res, 0)
    },
    handleCommand(val, id) {
      // console.log(val, id)
      this.currentId = id
      if (val === 'add') {
        // 添加
        // this.title = '新增部门'
        this.showDialog = true
      }
    }
  }
}
</script>

<style>
.app-container {
  padding: 20px 80px;
  font-size: 14px;
}
</style>
